
import 'package:get/get.dart';
import 'package:safetymonitor/app/data/entities/home/risk_tree_entity.dart';
import 'package:safetymonitor/modules/home/controller.dart';

class DeviceTreeController extends GetxController {
  /// 组织数据
  List<RiskTreeEntity> treeList = [];

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  /// 初始化数据列表
  Future<void> onInitData(RiskTreeEntity selectedEntity) async {
    this.selectedEntity = selectedEntity;
    if (treeList.isEmpty) {
        // 请求接口
        // final res = await HttpUtil().generateGetRequest<List<RiskTreeEntity>>(Interface.companyTree).execute();
        // res.onSuccess((result) {
        //   if (result.data != null) {
        //     print('result.data${result.data}');
        //     treeList = result.data!;
        //     update();
        //   }
        // });
      treeList = Get.find<HomeController>().treeList;
      update();
    }
  }
  /// 点击节点
  Future<void> clickOnNode(RiskTreeEntity treeEntity) async {
    treeEntity.isExpand = !treeEntity.isExpand!;
    selectedEntity = treeEntity;
    update();
  }
  /// 切换节点的展开/折叠状态
  //  这个方法接受一个 RiskTreeEntity 作为参数，切换该节点的 isExpand 属性。如果当前是展开状态，则折叠该节点，反之亦然。
  void toggleExpand(RiskTreeEntity treeEntity) {
    treeEntity.isExpand = !(treeEntity.isExpand ?? false);
    update();
  }
}
